package com.lmx.repoDomain.tmodel.business;

import io.katharsis.resource.annotations.JsonApiIncludeByDefault;
import io.katharsis.resource.annotations.JsonApiResource;
import io.katharsis.resource.annotations.JsonApiToMany;
import io.katharsis.resource.annotations.JsonApiToOne;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ForeignKey;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.lmx.repoDomain.tmodel.BaseEntity;
import com.lmx.repoDomain.tmodel.Dicttype;

/**
 * 机构表
 */
@JsonApiResource(type = "organization")
@Table(name = "tnb_organization")
@Entity
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Organization extends BaseEntity {

	private User lastUpdateUser;// 更新操作者userid
	private Integer createDateTime;// 创建时间
	private User createUser;
	private Integer lastUpdateDateTime;// 更新时间

	private String orgCode;// 编码
	private String orgName;// 名称
	private String orgShortName;// 简称
	private String remark;// 机构简介
	private String address;// 地址
	private String orgTel;// 电话
	private String legalPerson;// 法人
	private String licenseImg;// 营业执照

	// 联系人 联系电话
	private String linkMan;// 联系人
	private String linkManTel;// 联系人电话
	private String linkManQQ;// 联系人QQ
	private String linkManMail;// 联系人邮箱

	// 密保
	// private String secretQuestion;// 密保问题
	// private String secretAnswer;// 密保答案
	// 账户信息
	private User admin;

	private Dicttype type;// 类型
	private Integer order;// 顺序
	private Organization parent; // 父权限
	private List<Organization> children;

	// private TradeAccount account;
	//
	// @JsonApiToOne
	// @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
	// @JoinColumn(name = "account_id", foreignKey = @ForeignKey(name = "null"))
	// public TradeAccount getAccount() {
	// return account;
	// }
	//
	// public void setAccount(TradeAccount account) {
	// this.account = account;
	// }

	@Column(name = "createDateTime")
	public Integer getCreateDateTime() {
		return createDateTime;
	}

	public void setCreateDateTime(Integer createDateTime) {
		this.createDateTime = createDateTime;
	}

	@Column(name = "lastUpdateDateTime")
	public Integer getLastUpdateDateTime() {
		return lastUpdateDateTime;
	}

	public void setLastUpdateDateTime(Integer lastUpdateDateTime) {
		this.lastUpdateDateTime = lastUpdateDateTime;
	}

	@JsonApiToOne
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "createuser_id", foreignKey = @ForeignKey(name = "null"))
	public User getCreateUser() {
		return createUser;
	}

	public void setCreateUser(User createUser) {
		this.createUser = createUser;
	}

	@JsonApiToOne
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "lastupdateuser_id", foreignKey = @ForeignKey(name = "null"))
	public User getLastUpdateUser() {
		return lastUpdateUser;
	}

	public void setLastUpdateUser(User lastUpdateUser) {
		this.lastUpdateUser = lastUpdateUser;
	}

	@Column(name = "orgshortname")
	public String getOrgShortName() {
		return orgShortName;
	}

	public void setOrgShortName(String orgShortName) {
		this.orgShortName = orgShortName;
	}

	@Column(name = "legalperson")
	public String getLegalPerson() {
		return legalPerson;
	}

	public void setLegalPerson(String legalPerson) {
		this.legalPerson = legalPerson;
	}

	@Column(name = "licenseimg")
	public String getLicenseImg() {
		return licenseImg;
	}

	public void setLicenseImg(String licenseImg) {
		this.licenseImg = licenseImg;
	}

	@Column(name = "linkmanqq")
	public String getLinkManQQ() {
		return linkManQQ;
	}

	public void setLinkManQQ(String linkManQQ) {
		this.linkManQQ = linkManQQ;
	}

	@Column(name = "linkmanmail")
	public String getLinkManMail() {
		return linkManMail;
	}

	public void setLinkManMail(String linkManMail) {
		this.linkManMail = linkManMail;
	}

	@JsonApiToOne
	// @JsonApiIncludeByDefault
	@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
	@JoinColumn(name = "admin_id", foreignKey = @ForeignKey(name = "null"))
	@Fetch(FetchMode.JOIN)
	public User getAdmin() {
		return admin;
	}

	public void setAdmin(User admin) {
		this.admin = admin;
	}

	@Column(name = "linkman")
	public String getLinkMan() {
		return linkMan;
	}

	public void setLinkMan(String linkMan) {
		this.linkMan = linkMan;
	}

	@Column(name = "linkmantel")
	public String getLinkManTel() {
		return linkManTel;
	}

	public void setLinkManTel(String linkManTel) {
		this.linkManTel = linkManTel;
	}

	public Organization() {
	}

	@Column(name = "orgcode", length = 32)
	public String getOrgCode() {
		return orgCode;
	}

	public void setOrgCode(String orgCode) {
		this.orgCode = orgCode;
	}

	@Column(name = "orgname", length = 32)
	public String getOrgName() {
		return orgName;
	}

	public void setOrgName(String orgName) {
		this.orgName = orgName;
	}

	@JsonApiToOne
	@JsonApiIncludeByDefault
	@Fetch(FetchMode.JOIN)
	@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
	@JoinColumn(name = "orgtype_id", foreignKey = @ForeignKey(name = "null"))
	public Dicttype getType() {
		return type;
	}

	public void setType(Dicttype type) {
		this.type = type;
	}

	@Column(name = "orgorder")
	public Integer getOrder() {
		return order;
	}

	public void setOrder(Integer order) {
		this.order = order;
	}

	@JsonApiToOne
	@ManyToOne
	@JoinColumn(name = "parent_orgid", nullable = true, foreignKey = @ForeignKey(name = "null"))
	@NotFound(action = NotFoundAction.IGNORE)
	@JsonBackReference
	public Organization getParent() {
		return parent;
	}

	public void setParent(Organization parent) {
		this.parent = parent;
	}

	@JsonApiToMany
	@OneToMany(mappedBy = "parent")
	@JsonManagedReference
	public List<Organization> getChildren() {
		return children;
	}

	public void setChildren(List<Organization> children) {
		this.children = children;
	}

	@Column(name = "remark", length = 2000)
	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	@Column(name = "address")
	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	@Column(name = "orgtel")
	public String getOrgTel() {
		return orgTel;
	}

	public void setOrgTel(String orgTel) {
		this.orgTel = orgTel;
	}
}
